package com.lixin.smartclassroom.ui.home.tab3.fragment

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.drake.tooltip.toast
import com.lixin.smartclassroom.api.getzhutisource
import com.lixin.smartclassroom.databinding.FragmentTab1Binding
import com.lixin.smartclassroom.databinding.FragmentTab2Binding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.PagerAdapter
import com.lixin.smartclassroom.ui.home.tab1.activity.MessageAct
import com.lixin.smartclassroom.ui.home.tab1.activity.SearchAct
import com.lixin.smartclassroom.ui.home.tab2.activity.AddCourseAct
import com.lixin.smartclassroom.ui.home.tab3.activity.TotalScoreAct
import com.lixin.smartclassroom.ui.home.tab4.fragment.Tab4ChildFragment
import com.lixin.smartclassroom.ui.login.BindStudentAct
import com.lixinkeji.base.BaseFragment
import com.lixinkeji.extension.addIntArgument
import com.lixinkeji.extension.commonDialog
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description
 * Author: lwc
 * Date:2023/12/5 15:24
 */
class Tab3Fragment : BaseFragment<FragmentTab2Binding>() {
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = FragmentTab2Binding.inflate(inflater)

    private var totalScore=""

    override fun iniView() {
        initSlidingLayout()
    }

    private fun initSlidingLayout() {
        val titles = ArrayList<String>()
        val fragments = ArrayList<Fragment>()


        binding.apply {
            when (requireContext().user_role) {
                1 -> {
                    titles.add("我孩子参与的")
                    fragments.add(Tab3ChildFragment().addIntArgument("type", 2))
                    tvTotalScore.visible()
                    tvDetail.visible()
                    getTotalScoreData()
                }

                2 -> {
                    titles.add("我参与的")
                    fragments.add(Tab3ChildFragment().addIntArgument("type", 2))
                    tvTotalScore.visible()
                    tvDetail.visible()
                    getTotalScoreData()
                }

                3 -> {
                    titles.add("我创建的")
                    titles.add("我参与的")
                    fragments.add(Tab3ChildFragment().addIntArgument("type", 1))
                    fragments.add(Tab3ChildFragment().addIntArgument("type", 2))
                    binding.btnAdd.visible()
                    tvTotalScore.gone()
                    tvDetail.gone()
                }
            }

        }

        var mPageAdapter = PagerAdapter(childFragmentManager, fragments, titles)

        binding.apply {
            viewPager.adapter = mPageAdapter
            tabLayout.setViewPager(viewPager)
            tabLayout.onPageSelected(0)
        }

        receiveEvent<String>("showMessageCount") {
            binding.tvMessageCount.visible()
            binding.tvMessageCount.text = it
        }

    }

    /**
     *@Description 获取总得分
     *@date 创建时间2024/2/21
     *
     */
    private fun getTotalScoreData() {
        scopeNetLife {
            val baseBean = when(requireContext().user_role){
                1->getzhutisource("2",requireContext().child_id)
                else->getzhutisource("1")
            }
            if(baseBean.result=="0"){
                binding.tvTotalScore.text = "累计得分："+baseBean.source!!.toDouble().toInt().toString()+"分"
                totalScore = baseBean.source.toString()
            }else{
                toast(baseBean.resultNote.toString())
            }
        }

    }

    override fun onClick() {
        binding.apply {
            btnAdd.onClick {
                start(AddCourseAct::class.java) {
                    putExtra("type", "2")
                }
            }

            tvDetail.onClick {
                start(TotalScoreAct::class.java){
                    putExtra("totalScore",totalScore)
                }
            }

            imgSearch.onClick {
                start(SearchAct::class.java)
            }

            rlMessage.onClick {
                start(MessageAct::class.java)
            }
        }

    }
}